#include "contact.h"
////////////////////////
//
//  Constructeur
//
////////////////////////
Contact::Contact()
{
    m_telephone1 = QString("");
    m_telephone2 = QString("");
    m_adresseMail = QString("");
    m_nom = QString("");
    m_prenom = QString("");
    m_priorite = -1;
}
Contact::Contact(QString telephone1, QString telephone2, QString adresseMail, QString nom, QString prenom)
{
    m_telephone1 = telephone1;
    m_telephone2 = telephone2;
    m_adresseMail = adresseMail;
    m_nom = nom;
    m_prenom = prenom;
    m_priorite = 1;
}
Contact::Contact(QString telephone1, QString telephone2, QString adresseMail, QString nom, QString prenom, int priorite)
{
    m_telephone1 = telephone1;
    m_telephone2 = telephone2;
    m_adresseMail = adresseMail;
    m_nom = nom;
    m_prenom = prenom;
    m_priorite = priorite;
}
Contact::Contact(QSqlQuery query, int i)
{
    if (!i > 0)
    {
        m_id = query.value(i).toInt();
        m_clientId = query.value(i + 1).toInt();
        i += 2;
        qDebug() << "id contact " << m_id;
    }
    m_telephone1 = query.value(i).toString();
    m_telephone2 = query.value(i + 1).toString();
    m_adresseMail = query.value(i + 2).toString();
    m_nom = query.value(i + 3).toString();
    m_prenom = query.value(i + 4).toString();
    m_priorite = query.value(i + 5).toInt();


}
void Contact::BindValues(QSqlQuery &query, QStringList champs)
{
    int i = 0;
    if (champs.contains("client_id"))
    {
        query.bindValue(":" + champs.at(i), this->getClientId());
        i++;
    }

    query.bindValue(":" + champs.at(i), this->getTelephone1());
    query.bindValue(":" + champs.at(i + 1), this->getTelephone2());
    query.bindValue(":" + champs.at(i + 2), this->getAdresseMail());
    query.bindValue(":" + champs.at(i + 3), this->getNom());
    query.bindValue(":" + champs.at(i + 4), this->getPrenom());
    query.bindValue(":" + champs.at(i + 5), this->getPriorite());

}
////////////////////////
//
//  Accesseur
//
////////////////////////
void Contact::setClientId(int clientId)
{
    m_clientId = clientId;
}
void Contact::setTelephone1(QString telephone1)
{
    m_telephone1 = telephone1;
}
void Contact::setTelephone2(QString telephone2)
{
    m_telephone2 = telephone2;
}
void Contact::setAdresseMail(QString adresseMail)
{
    m_adresseMail = adresseMail;
}
void Contact::setNom(QString nom)
{
    m_nom = nom;
}
void Contact::setPrenom(QString prenom)
{
    m_prenom = prenom;
}
void Contact::setPriorite(int priorite)
{
    m_priorite = priorite;
}
int Contact::getClientId()const
{
    return m_clientId;
}
QString Contact::getTelephone1()const
{
    return m_telephone1;
}
QString Contact::getTelephone2()const
{
    return m_telephone2;
}
QString Contact::getTelephone1WithSpaces()const
{
    QString tel1 = m_telephone1;
    if (tel1.length() == 10)
    {
        tel1.insert(2, " ");
        tel1.insert(5, " ");
        tel1.insert(8, " ");
        tel1.insert(11, " ");
    }

    return tel1;
}
QString Contact::getTelephone2WithSpaces()const
{
    return m_telephone2;
}
QString Contact::getAdresseMail()const
{
    return m_adresseMail;
}
QString Contact::getNom()const
{
    if (m_nom.isNull())
        return "";
    return m_nom;
}
QString Contact::getPrenom()const
{
    return m_prenom;
}
int Contact::getPriorite()const
{
    return m_priorite;
}
QString Contact::NomEtPrenom()const
{
    QString s = "";
    s = m_nom;
    if (!s.isEmpty())
        s += ", ";
    s += m_prenom;

    if (!m_telephone1.isEmpty())
    {
        if (!s.isEmpty())
            s += "\n";
        s += getTelephone1WithSpaces();
    }
    else if (!m_adresseMail.isEmpty())
    {
        if (!s.isEmpty())
            s += "\n";
        s = m_adresseMail;
    }

    return s;
}
////////////////////////
//
//  static SQL
//
////////////////////////
const QString Contact::NomChampClientId = QString("client_id");
const QString Contact::NomChampTelephone1 = QString("telephone1");
const QString Contact::NomChampTelephone2 = QString("telephone2");
const QString Contact::NomChampAdresseMail = QString("adresseMail");
const QString Contact::NomChampNom = QString("nom");
const QString Contact::NomChampPrenom = QString("prenom");
const QString Contact::NomChampPriorite = QString("priorite");

QStringList Contact::ListeChampSQL()
{
    QStringList l;
    l << NomChampId << NomChampClientId << NomChampTelephone1 << NomChampTelephone2 << NomChampAdresseMail
      << NomChampNom << NomChampPrenom << NomChampPriorite;
    return l;
}
QStringList Contact::ListeChampSQLSansId()
{
    QStringList l;
    l = ListeChampSQL();
    l.removeFirst();
    l.removeFirst();
    return l;
}

QStringList Contact::ListeTypeDataSQL()
{
   // INTEGER PRIMARY KEY ASC

    QStringList l;
    l << TypeVarId << "SMALLINT UNSIGNED NOT NULL" << "varchar(30)" << "varchar(30)" << "varchar(80)"
      << "varchar(30)" << "varchar(30)" << "tinyint";
    return l;
}
QStringList Contact::ListeTypeDataSQLSansId()
{
    QStringList l;
    l = ListeTypeDataSQL();
    l.removeFirst();
    l.removeFirst();
    return l;
}
QStringList Contact::SQLWhere()const
{
    QStringList l;
    l <<  NomChampClientId << QString::number(m_clientId) << NomChampNom << m_nom << NomChampPrenom << m_prenom;
    return l;
}
QString Contact::SQLSet()const
{
    return  NomChampClientId + "='" + m_clientId + "', " + NomChampNom + "='" + m_nom + "', " + NomChampPrenom + "='" + m_prenom + "', " + NomChampTelephone1
            + "='" + m_telephone1 + "', " + NomChampTelephone2 + "='" + m_telephone2
            + "', " + NomChampAdresseMail + "='" + m_adresseMail + "', " + NomChampPriorite + "='" + m_priorite + "'";
}
QString Contact::SetQuery()const
{
    return  NomChampNom + "='" + NomChampNom.toLower() + "', "
            + NomChampPrenom + "='" + NomChampPrenom.toLower() + "', "
            + NomChampTelephone1 + "='" + NomChampTelephone1.toLower() + "', "
            + NomChampTelephone2 + "='" + NomChampTelephone2.toLower() + "', "
            + NomChampAdresseMail + "='" + NomChampAdresseMail.toLower() + "', "
            + NomChampPriorite + "='" + NomChampPriorite.toLower() + "'";
}
QStringList Contact::SetBindValues()const
{
    QStringList l;
    l << NomChampClientId << QString::number(m_clientId)
      << NomChampNom.toLower() << m_nom
      << NomChampPrenom.toLower() << m_prenom
      << NomChampTelephone1.toLower() << m_telephone1
      << NomChampTelephone2.toLower() << m_telephone2
      << NomChampAdresseMail.toLower() << m_adresseMail
      << NomChampPriorite << QString::number(m_priorite)
      << "id" << QString::number(m_id);
    return l;
}
QStringList Contact::ChampDeTri()
{
    QStringList l;
    l << NomChampPriorite << NomChampNom << NomChampPrenom;
    return l;
}
QString Contact::SQLForeignKey()
{
    return ", FOREIGN KEY (" + NomChampClientId + ") REFERENCES clients(id)"; // CONSTRAINT fk_contact_numero
}
////////////////////////
//
//  Operateur
//
////////////////////////
bool Contact::operator==(Contact const& contact)
{
    return (m_nom.toLower() == contact.getNom().toLower()) && (m_prenom.toLower() == contact.getPrenom().toLower())
            && (m_telephone1.toLower() == contact.getTelephone1().toLower()) && (m_telephone2.toLower() == contact.getTelephone2().toLower())
            && (m_adresseMail.toLower() == contact.getAdresseMail().toLower());
}
bool Contact::isNULL()const
{
    return (m_telephone1.isEmpty() && m_telephone2.isEmpty() && m_adresseMail.isEmpty() && m_nom.isEmpty() && m_prenom.isEmpty());

}
